package com.liry.zk;

import java.util.concurrent.TimeUnit;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;

/**
 * curator客户端测试
 *
 * @author ALI
 * @since 2022/12/22
 */
public class TestZk2 {

    private static final String CONNECT_STR = "192.168.0.110:2181";

    private static final int TIME_OUT = 30000;

    public static void main(String[] args) throws Exception {


    }

    /**
     * 监听测试
     */
    private static void watcher() throws Exception {
        // 重试配置
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.builder()
                                                         .connectString(CONNECT_STR)
                                                         .sessionTimeoutMs(TIME_OUT)
                                                         .connectionTimeoutMs(TIME_OUT)
                                                         .retryPolicy(retryPolicy)
                                                         .build();
        client.start();
        NodeCache nodeCache = new NodeCache(client, "/test2");
        nodeCache.start();
        nodeCache.getListenable().addListener(() -> {
            System.out.println("数据变更");
            ChildData currentData = nodeCache.getCurrentData();
            System.out.println(new String(currentData.getData()));
        });


        PathChildrenCache pathChildrenCache = new PathChildrenCache(client, "/test2", true);
        pathChildrenCache.start();
        pathChildrenCache.getListenable().addListener((curatorFramework, pathChildrenCacheEvent) -> {
            switch (pathChildrenCacheEvent.getType()) {
                case CHILD_ADDED:
                    System.out.println("新增子节点");
                    System.out.println("新增节点的数据：" + new String(pathChildrenCacheEvent.getData().getData()));
                    break;
                case CHILD_UPDATED:
                    System.out.println("更新子节点");
                    System.out.println("更新节点的数据：" + new String(pathChildrenCacheEvent.getData().getData()));
                    break;
                default:
                    break;
            }
        });


        TreeCache treeCache = new TreeCache(client, "/test2");
        treeCache.start();

        treeCache.getListenable().addListener((curatorFramework, treeCacheEvent) -> {
            switch (treeCacheEvent.getType()) {
                case NODE_UPDATED:
                    System.out.println("更新节点");
                    System.out.println("更新节点的数据：" + new String(treeCacheEvent.getData().getData()));
                    break;
                default:
                    break;
            }
        });

        TimeUnit.SECONDS.sleep(6000);
    }

    /**
     * 基本操作测试
     */
    private static void baseOperation() throws Exception {
        // 重试配置
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.builder()
                                                         .connectString(CONNECT_STR)
                                                         .sessionTimeoutMs(TIME_OUT)
                                                         .connectionTimeoutMs(TIME_OUT)
                                                         .retryPolicy(retryPolicy)
                                                         .build();
        client.start();

        // 创建节点
        String path = client.create().forPath("/test2", "xxx".getBytes());
        System.out.println(path);

        // 查询节点数据
        byte[] bytes = client.getData().forPath("/test2");
        System.out.println("获取到的数据：" + new String(bytes));

        // 修改节点数据
        Stat stat = client.setData().forPath("/test2", "777".getBytes());
        bytes = client.getData().forPath("/test2");
        System.out.println("修改后的数据：" + new String(bytes));

        // 删除节点
        client.delete().forPath("/test2");
        // 节点不存在，这里报异常
        bytes = client.getData().forPath("/test2");
        System.out.println("删除后的数据：" + new String(bytes));
    }


}
